package com.demo1;

import java.util.ArrayList;
import java.util.List;

/**
 * 中介者模式
 * @author zhoujun
 * @date 2025/3/26 23:39
 */
public class ConcreteMediator implements Mediator {

    private List<Colleague> colleagueList = new ArrayList<>();

    /**
     * 注册一个同事对象
     * @param colleague
     */
    @Override
    public void register(Colleague colleague) {
        if (!colleagueList.contains(colleague)){
            colleagueList.add(colleague);
        }
    }

    /**
     * 转发消息给其它对象
     * @param colleague
     * @param message
     */
    @Override
    public void relay(Colleague colleague, String message) {
        for (Colleague c : colleagueList) {
            if (!c.equals(colleague)){
                c.receive(message);
            }
        }
    }
}
